//
//  EnterDetailsViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-4-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "EnterDetailsViewController.h"
#import "DetailsViewController.h"

@interface EnterDetailsViewController ()

@end

@implementation EnterDetailsViewController

@synthesize heatsAndCools, coolsOnly, insulationYes, insulationNo, westWindowYes,westWindowNo;
@synthesize widthSlider, lengthSlider;
@synthesize areaLabel,widthLabel,lengthLabel;
@synthesize widthSliderMax,widthSliderMin,lengthSliderMax,lengthSliderMin;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Enter details";
        widthString = @"Width = ";
        lengthString = @"Length = ";
        meterString = @"m";
        //squareString = @"㎡";
        ftString = @"ft";
        iWidth = 5;
        iLength = 5;
        isInMeterMode = YES;
        
        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"Back";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        
        hasCheckHeats = NO;
        hasCheckInsulation = NO;
        hasCheckWindow = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.    
    UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"segment_tools.png"]
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(showMetricsDialog)];
    self.navigationItem.rightBarButtonItem = infoItem;
}

- (void)showMetricsDialog
{
    // open a dialog with two custom buttons
    NSString *title = @"How would you like to enter your room measurement?";
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                             delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                                    otherButtonTitles:@"In Metres", @"In Feet", nil];
	//actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
	//actionSheet.destructiveButtonIndex = 1;	// make the second button red (destructive)
    actionSheet.backgroundColor = [UIColor lightGrayColor]; 
    [actionSheet showInView:self.tabBarController.view];
    
}

- (void)setButtonHighlighted:(UIButton*)button
{
    button.highlighted = YES;
    button.enabled = NO;
}

- (IBAction)toggleHeatsAndCools:(id)sender
{
    hasCheckHeats = YES;
    if(sender == self.heatsAndCools){
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.heatsAndCools afterDelay:0];
        [self.coolsOnly setHighlighted:NO];
        self.coolsOnly.enabled = YES;
    }else {
        [self.heatsAndCools setHighlighted:NO];
        self.heatsAndCools.enabled = YES;
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.coolsOnly afterDelay:0];
    }

}

- (IBAction)toggleInsulation:(id)sender
{
    hasCheckInsulation = YES;
    if(sender == self.insulationYes){
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.insulationYes afterDelay:0];
        [self.insulationNo setHighlighted:NO];
        self.insulationNo.enabled = YES;
    }else {
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.insulationNo afterDelay:0];
        [self.insulationYes setHighlighted:NO];
        self.insulationYes.enabled = YES;
    }
    
}

- (IBAction)toggleWestWindow:(id)sender
{
    hasCheckWindow = YES;
    if(sender == self.westWindowYes){
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.westWindowYes afterDelay:0];
        [self.westWindowNo setHighlighted:NO];
        self.westWindowNo.enabled = YES;
    }else {
        [self performSelector:@selector(setButtonHighlighted:) withObject:self.westWindowNo afterDelay:0];
        [self.westWindowYes setHighlighted:NO];
        self.westWindowYes.enabled = YES;
    }
    
}

- (IBAction)widthSliderAction:(id)sender
{
	UISlider* durationSlider = sender;
	float fWidth = [durationSlider value];
    NSInteger newWidth = fWidth;
    if(newWidth != iWidth){
        iWidth = newWidth;
        NSString *temp = isInMeterMode ? meterString : ftString;
        self.widthLabel.text = [NSString stringWithFormat:@"%@%d%@", widthString, iWidth, temp];
        self.areaLabel.text = [NSString stringWithFormat:@"%d%@", iWidth*iLength, temp];
    }
    
}

- (IBAction)lengthSliderAction:(id)sender
{
	UISlider* durationSlider = sender;
	float fLength = [durationSlider value];
    NSInteger newLength = fLength;
    if(newLength != iLength){
        iLength = newLength;
        NSString *temp = isInMeterMode ? meterString : ftString;
        self.lengthLabel.text = [NSString stringWithFormat:@"%@%d%@", lengthString, iLength, temp];
        self.areaLabel.text = [NSString stringWithFormat:@"%d%@", iWidth*iLength, temp];
    }

}

- (IBAction)goToDetails:(id)sender
{
    if (!hasCheckHeats) {
        [self alert:@"Please select if you need an Air Conditioner that Heats and Cools, or Cools Only."];
        return;
    }
    if (!hasCheckInsulation) {
        [self alert:@"Please select if your house has insulation."];
        return;
    }
    if (!hasCheckWindow) {
        [self alert:@"Please select if your house has west facing windows"];
        return;
    }
    
    if(detailsViewController == nil){
        detailsViewController = [[DetailsViewController alloc]
                                     initWithNibName:@"DetailsViewController" bundle:nil];
    }
    [[self navigationController] pushViewController:detailsViewController animated:YES];
}

- (void)alert:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    
	[alertView show];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	switch (buttonIndex) {
        case 0:
            isInMeterMode = YES;
            iLength = 5;
            iWidth = 5;
            
            self.widthSliderMin.text = [NSString stringWithFormat:@"1%@", meterString];
            self.widthSliderMax.text = [NSString stringWithFormat:@"12%@", meterString];
            self.lengthSliderMin.text = [NSString stringWithFormat:@"1%@", meterString];
            self.lengthSliderMax.text = [NSString stringWithFormat:@"12%@", meterString];

            
            self.widthSlider.maximumValue = 12;
            self.lengthSlider.maximumValue = 12;
            
            self.widthSlider.value = iWidth;
            self.lengthSlider.value = iLength;
            
            self.widthLabel.text = [NSString stringWithFormat:@"%@%d%@", widthString, iWidth, meterString];
            self.lengthLabel.text = [NSString stringWithFormat:@"%@%d%@", lengthString, iLength, meterString];
            self.areaLabel.text = [NSString stringWithFormat:@"%d%@", iWidth*iLength, meterString];            
            break;
        case 1:
            isInMeterMode = NO;
            iLength = 10;
            iWidth = 10;
            
            self.widthSliderMin.text = [NSString stringWithFormat:@"1%@", ftString];
            self.widthSliderMax.text = [NSString stringWithFormat:@"12%@", ftString];
            self.lengthSliderMin.text = [NSString stringWithFormat:@"1%@", ftString];
            self.lengthSliderMax.text = [NSString stringWithFormat:@"12%@", ftString];
            
            self.widthSlider.maximumValue = 40;
            self.lengthSlider.maximumValue = 40;
            
            self.widthSlider.value = iWidth;
            self.lengthSlider.value = iLength;
            
            self.widthLabel.text = [NSString stringWithFormat:@"%@%d%@", widthString, iWidth, ftString];
            self.lengthLabel.text = [NSString stringWithFormat:@"%@%d%@", lengthString, iLength, ftString];
            self.areaLabel.text = [NSString stringWithFormat:@"%d%@", iWidth*iLength, ftString];
            break;
        default:
            break;
    }
}
@end
